package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.Kefu;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Kefu
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Kefu
 */
public class KefuDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(KefuDAO.class);
    // property constants
    public static final String BUYER_ID = "buyerId";
    public static final String KEFU_NICKNAME = "kefuNickname";

    public Kefu save(Kefu transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<Kefu> saveList(List<Kefu> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Kefu temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(Kefu persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Kefu findById(java.lang.String id)
    {
        log.debug("getting Kefu instance with id: " + id);
        try
        {
            Kefu instance = (Kefu) getSession().get("com.qianhui.pojo.Kefu", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Kefu instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Kefu as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByKefuNickname(Object kefuNickname)
    {
        return findByProperty(KEFU_NICKNAME, kefuNickname);
    }

    public List findAll()
    {
        log.debug("finding all Kefu instances");
        try
        {
            String queryString = "from Kefu k order by k.kefuAddTime desc";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Kefu findByBuyerId(String buyerId)
    {
        try
        {
            String queryString = "from Kefu k where k.buyerId=:buyerId";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter("buyerId", buyerId);
            List tempList = queryObject.list();
            if (tempList != null && tempList.size() > 0)
            {
                return (Kefu) tempList.get(0);
            }
            else
            {
                return null;
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}